From e5f37b003d93a17c2a9958697269b2d3228d8830 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 4 Mar 2026 23:06:35 -0800 Subject: tuning --- src/routes/(app)/invoice/[id]/+layout.server.ts | 61 --------------------- src/routes/(app)/invoice/[id]/+layout.ts | 66 +++++++++++++++++++++++ src/routes/(app)/invoice/[id]/+page.svelte | 4 +- src/routes/(app)/invoice/[id]/tip/+page.server.ts | 5 +- 4 files changed, 71 insertions(+), 65 deletions(-) delete mode 100644 src/routes/(app)/invoice/[id]/+layout.server.ts create mode 100644 src/routes/(app)/invoice/[id]/+layout.ts (limited to 'src/routes/(app)/invoice/[id]') diff --git a/src/routes/(app)/invoice/[id]/+layout.server.ts b/src/routes/(app)/invoice/[id]/+layout.server.ts deleted file mode 100644 index 24b7f1b0..00000000 --- a/src/routes/(app)/invoice/[id]/+layout.server.ts +++ /dev/null @@ -1,61 +0,0 @@ -import getRates from "$lib/rates"; -import { get } from "$lib/utils"; -import { redirect } from "@sveltejs/kit"; - -export async function load({ cookies, depends, params, url, parent }) { - depends("app:invoice"); - - const token = cookies.get("token"); - let { subject, user } = await parent(); - const { id } = params; - let invoice; - - if (id === "lnurl") { - const rates = await getRates(); - invoice = { - aid: user.id, - uid: user.id, - amount: 0, - tip: 0, - rate: rates[user?.currency || "USD"], - text: "", - hash: "", - type: "lightning", - items: [], - memo: "", - pending: 0, - received: 0, - user: { id: user.id, username: user.username, currency: user.currency }, - }; - } else if (id) { - invoice = await get(`/invoice/${id}`); - const options = !!url.searchParams.get("options"); - let { amount, pending, received } = invoice; - amount = parseInt(amount); - - const paid = - (!amount && (pending || received)) || - (amount > 0 && (pending >= amount || received >= amount)); - if (paid && !url.pathname.endsWith("paid")) { - redirect(307, `/invoice/${id}/paid` + (options ? "?options=true" : "")); - } - - subject = invoice.user; - - const aid = cookies.get("aid") || user?.id; - if ( - user && - invoice.uid !== user?.id && - !(url.pathname.includes("tip") || url.pathname.includes("memo")) && - !options - ) { - if (invoice.prompt && invoice.tip === null) redirect(307, `/invoice/${id}/tip`); - else { - redirect(307, `/send/invoice/${id}`); - } - } - } - - const theme = cookies.get("theme") || "light"; - return { id, invoice, subject, user, token, theme }; -} diff --git a/src/routes/(app)/invoice/[id]/+layout.ts b/src/routes/(app)/invoice/[id]/+layout.ts new file mode 100644 index 00000000..411486c7 --- /dev/null +++ b/src/routes/(app)/invoice/[id]/+layout.ts @@ -0,0 +1,66 @@ +import { get } from "$lib/utils"; +import { redirect } from "@sveltejs/kit"; + +export async function load({ params, url, parent, depends }) { + depends("app:invoice"); + + const { user, token, theme } = await parent(); + const { id } = params; + let invoice; + let subject; + + if (id === "lnurl") { + subject = user; + invoice = { + aid: user.id, + uid: user.id, + amount: 0, + tip: 0, + rate: 0, + text: `${user.username}@${url.host}`, + hash: "", + type: "lightning", + items: [], + memo: "", + pending: 0, + received: 0, + user: { id: user.id, username: user.username, currency: user.currency }, + }; + } else if (id) { + invoice = await get(`/invoice/${id}`); + const options = !!url.searchParams.get("options"); + let { amount, pending, received } = invoice; + amount = parseInt(amount); + + const paid = + (!amount && (pending || received)) || + (amount > 0 && (pending >= amount || received >= amount)); + if (paid && !url.pathname.endsWith("paid")) { + redirect(307, `/invoice/${id}/paid` + (options ? "?options=true" : "")); + } + + subject = invoice.user; + + if ( + user && + invoice.uid !== user?.id && + !(url.pathname.includes("tip") || url.pathname.includes("memo")) && + !options + ) { + if (invoice.prompt && invoice.tip === null) redirect(307, `/invoice/${id}/tip`); + else { + redirect(307, `/send/invoice/${id}`); + } + } + } + + let qrDataUrl: string | undefined; + if (invoice?.text) { + try { + const QRCode = await import("qrcode"); + qrDataUrl = await QRCode.toDataURL(invoice.text, { width: 600, margin: 1 }); + } catch {} + } + + return { id, invoice, subject, user, token, theme, qrDataUrl }; +} diff --git a/src/routes/(app)/invoice/[id]/+page.svelte b/src/routes/(app)/invoice/[id]/+page.svelte index 988ba647..7ce7c4e8 100644 --- a/src/routes/(app)/invoice/[id]/+page.svelte +++ b/src/routes/(app)/invoice/[id]/+page.svelte @@ -32,7 +32,7 @@ let user = $derived(data.user); let hasArk = $derived(data.hasArk); let invoice = $derived(data.invoice); - let src = $derived(data.src); + let qrDataUrl = $derived(data.qrDataUrl); let { aid, amount, rate, tip, hash, text, type } = $derived(invoice); let memo = $state(); let { username, currency } = $derived(invoice.user); @@ -185,7 +185,7 @@